import sys
import os
import ast
import subprocess
import io
import os.path
import random

# sys.executable

# 设置输出为中文编码
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', line_buffering=True)
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', line_buffering=True)
# 获取当前目录，仅限打包后
if getattr(sys, 'frozen', False):
    base_dir = os.path.dirname(sys.executable)
else:
    base_dir = os.path.join(os.path.dirname(__file__),'dist')

myDist = {1:'一',2:'二',3:'三',4:'四',5:'五',6:'六',7:'七',8:'八',9:'九'}


def checkin():
    arguments = {}
    try:
        # 取出参数
        for i in range(len(sys.argv)):
            if sys.argv[i] == "--help":
                print("""
                --help: 显示帮助信息
                --count: 设置输出算式数量
                --col: 设置输出列数
                --rangemin: 乘数范围最小值
                --rangemax: 乘数范围最大值
                --title: 添加标题
                """)
                sys.exit()
            elif sys.argv[i] == "--version":
                print("version: 1.0.0")
                sys.exit()
            elif sys.argv[i] == "--count":
                arguments["count"] = sys.argv[i+1]
            elif sys.argv[i] == "--col":
                arguments["col"] = sys.argv[i+1]
            elif sys.argv[i] == "--title":
                arguments["title"] = sys.argv[i+1]
            elif sys.argv[i] == "--rangemin":
                arguments["rangemin"] = sys.argv[i+1]
            elif sys.argv[i] == "--rangemax":
                arguments["rangemax"] = sys.argv[i+1]
    except Exception as e:
        return {"status": False, "data": str(e)}

    # 检查需要的参数是否都在里面
    for i in ['count', 'col', 'rangemin', 'rangemax']:
        if 'title' not in arguments.keys():
            arguments["title"] = "title"
            continue
        if 'rangemin' not in arguments.keys():
            arguments["rangemin"] = "1"
            continue
        if 'rangemax' not in arguments.keys():
            arguments["rangemax"] = "10"
            continue
        if i not in arguments.keys():
            return {"status": False, "data": f"缺少参数{i}"}
    
    # 尝试转换参数
    try:
        arguments["count"] = int(arguments["count"])
        arguments["col"] = int(arguments["col"])
        arguments["rangemin"] = int(arguments["rangemin"])
        arguments["rangemax"] = int(arguments["rangemax"])
    except Exception as e:
        return {"status": False, "data": str(e)}
    
    # 如果一切无误，则返回参数
    return {"status": True, "data": arguments}

"""把任意可迭代对象按宽度 n 切片，返回列表套列表。"""
# def chunk(seq, n: int):
#     return [seq[i:i+n] for i in range(0, len(seq), n)]

def main(data):
    list1 =list( range(data['rangemin'],data['rangemax']+1))
    count1 = random.choice(list1) # 乘数1
    _count1 = myDist[count1]
    list1.pop(list1.index(count1))
    count2 = random.choice(list1) # 乘数2
    _count2 = myDist[count2]
    res = count1*count2
    if res < 10:
      res = f"得{myDist[res]}"
    elif res == 10:
      res = "一十"
    elif res > 10 and res < 20:
      res = f"十{myDist[res%10]}"
      pass
    else:
      if res % 10 == 0:
        res = f"{myDist[res//10]}十"
      else:
        res = f"{myDist[res//10]}十{myDist[res%10]}"

    pos = random.randint(0,2)
    if pos == 0:
      _count1 = "(    )"
    elif pos == 1:
      _count2 = "(    )"
    else:
      res = "(    )"
    
    if count1 <= count2:    
      return f"{_count1}{_count2}{res}"
    else:
      return f"{_count2}{_count1}{res}"
    
if '__main__' == __name__:
    data = checkin()
    if data["status"]:
        for i in range(data["data"]["count"]):
            if (i+1)%data["data"]['col'] == 0:
              print(main(data=data["data"]))
            else:
               print(main(data=data["data"]),end=",")
    else:
        print(data["data"])
    